<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>空间分析服务-里程定线</title>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript" src="../../dist/include-leaflet.js"></script>
<script type="text/javascript">
    var host = window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090";
    var map, queryBySQLParams, queryBySQLService, resultLayer,
        pointsList, routeLine, routeLocatorParameters_line, routeLocatorService,
        baseUrl = host + "/iserver/services/map-changchun/rest/maps/长春市区图",
        serviceUrl = host + "/iserver/services/spatialanalyst-changchun/restjsr/spatialanalyst";
    map = L.map('map', {
        crs: L.CRS.NonEarthCRS({
            bounds: L.bounds([48.4, -7668.25], [8958.85, -55.58]),
            origin: L.point(48.4, -55.58)
        }),
        center: [-3861.91, 4503.62],
        maxZoom: 18,
        zoom: 2
    });
    L.supermap.tiledMapLayer(baseUrl, {noWrap: true}).addTo(map);
    routeCalculateMeasureProcess();

    function routeCalculateMeasureProcess() {
        //通过SQL查询的方法建立路由,并添加到地图上
        queryBySQLService = L.supermap.queryService(baseUrl);
        queryBySQLParams = new SuperMap.QueryBySQLParameters({
            queryParams: [
                new SuperMap.FilterParameter({
                    name: "RouteDT_road@Changchun",
                    attributeFilter: "RouteID=1690"
                })
            ]
        });
        queryBySQLService.queryBySQL(queryBySQLParams, function (SQLQueryServiceResult) {
            var result = SQLQueryServiceResult.result;
            if (result && result.recordsets && result.recordsets[0].features) {
                var resultFeatures = result.recordsets[0].features;
                resultLayer = L.geoJSON(resultFeatures, {color: "red"}).addTo(map);
                //将形成路由的点提出来，为了构造下面里程定线服务参数的sourceRoute
                pointsList = [];
                var routeObj = resultFeatures.features[0].geometry.coordinates[0];
                for (var i = 0; i < routeObj.length; i++) {
                    pointsList.push([routeObj[i][1], routeObj[i][0], routeObj[i][2]])
                }
                routeLine = L.polyline(pointsList);
                //里程定线服务
                routeLocatorParameters_line = new SuperMap.RouteLocatorParameters({
                    "sourceRoute": routeLine,
                    "type": "LINE",
                    "startMeasure": 10,
                    "endMeasure": 800,
                    "isIgnoreGap": true
                });
                routeLocatorService = L.supermap.spatialAnalystService(serviceUrl);
                routeLocatorService.routeLocate(routeLocatorParameters_line, function (routeLocatorServiceResult) {
                    if (routeLocatorServiceResult.result && routeLocatorServiceResult.result.resultGeometry) {
                        L.geoJSON(routeLocatorServiceResult.result.resultGeometry).addTo(map);
                    } else {
                        alert(routeLocatorServiceResult.error.errorMsg);
                    }
                });
            } else {
                alert(SQLQueryServiceResult.error.errorMsg);
            }
        });
    }
</script>
</body>
</html>